package cn.homework.web.servlet;

import cn.homework.domain.Payment;
import cn.homework.service.PaymentService;
import cn.homework.service.impl.PaymentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author ACT
 */
@WebServlet("/paymentServlet")
public class PaymentServlet extends HttpServlet {
    private PaymentService service = new PaymentServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String demand = request.getParameter("demand");
        switch (demand) {
            case "pay": {
                paymentMethodShow(request, response);
                break;
            }
            case "show": {
                paymentShow(request, response);
                break;
            }
            case "add": {
                paymentAdd(request, response);
                break;
            }
            case "delete": {
                paymentDel(request, response);
                break;
            }
            case "query": {
                paymentFindByCondition(request, response);
                break;
            }
            default:
                break;
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    /**
     * 支付,跳转到订单页面
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void paymentMethodShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Payment> paymentMethodList = service.findPaymentMethod();
        HttpSession session = request.getSession();
        session.setAttribute("paymentMethodList", paymentMethodList);
            response.sendRedirect(request.getContextPath() + "/CommodityPaymentInterface.jsp");
    }

    /**
     * 所有支付方式展示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void paymentShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Payment> paymentAllList = service.findPaymentMethod();
        HttpSession session = request.getSession();
        session.setAttribute("paymentAllList", paymentAllList);
        response.sendRedirect(request.getContextPath() + "/BackstagePaymentShow.jsp");
    }

    /**
     * 添加支付方式
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void paymentAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        Payment payment = new Payment();
        try {
            BeanUtils.populate(payment, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        service.addPayment(payment);
        paymentShow(request, response);
    }

    /**
     * 删除选定的支付方式
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void paymentDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        service.delPaymentById(id);
        paymentShow(request, response);
    }

    /**
     * 根据条件查询支付方式
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void paymentFindByCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        List<Payment> paymentList = service.findPaymentCondition(map);
        HttpSession session = request.getSession();
        session.setAttribute("paymentList", paymentList);
        response.sendRedirect(request.getContextPath() + "/BackstagePaymentQuery.jsp");
    }
}
